package com.my.content.ui_homework.fragment.FirstPage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.my.content.ui_homework.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Lenovo on 2016/11/13.
 */

public class FirstPageAdapter extends BaseAdapter {
    private List<FirstPages.RowsBean> mList;
    private Context mContext = null;
    public FirstPageAdapter(Context context) {
        this.mContext = context;
    }


    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.first_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        FirstPages.RowsBean data = mList.get(position);
        holder.first_pager_item_title.setText(data.getName());//内容
        holder.first_pager_item_price.setText("¥"+(int) data.getNewPrice());//价格
        holder.first_pager_item_much.setText("已卖出"+data.getSaleTotal()+"件");//作者
        return convertView;
    }

    public void setData(List<FirstPages.RowsBean> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    class ViewHolder {
        private TextView first_pager_item_title, first_pager_item_price,first_pager_item_much;
        public ViewHolder(View view) {
            first_pager_item_title = (TextView) view.findViewById(R.id.first_item_title);
            first_pager_item_price = (TextView) view.findViewById(R.id.first_item_price);
            first_pager_item_much = (TextView) view.findViewById(R.id.first_item_much);
        }
    }

    public FirstPages parseJsontoBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<FirstPages>() {
        }.getType());
    }
}
